
#include "gkt_board.h"
#include "gkt_driver.h"
#include "gkt_debug.h"
#include "gkt_gpio.h"
#include "gkt_adc.h"
#include "g_adc.h"


#define ADC_ST_INIT	(1U << 0)
#define ADC_ST_CONFIG	(1U << 2)
#define ADC_ST_ENABLE	(1U << 3)

typedef struct _adc_instance {
	uint32_t	dev_id;
	uint32_t 	status;
	uint32_t 	scale_type;/*700MV:3 1200MV:2 2400:3MV*/
	uint32_t 	mode;
	uint32_t	channel;
} adc_instance_s;

typedef struct _adc {
	int	init;
	adc_instance_s	instances[GKT_ADC_MAXNUMS];
} adc_s;
static adc_s s_adc;


int gkt_adc_standby(void)
{
	int retval=GKT_SUCCESS;
	
#if defined(GKT_CONFIG_ADC_CTRL_PIN)
	gkt_printf("adc ctl pin output low\n");
	gkt_gpio_set_pin_level(GKT_CONFIG_ADC_CTRL_PIN, 0);
#endif

#if defined(GKT_CONFIG_FACE_ID_POWER_CTRL_PIN)
	gkt_printf("adc/face ctl pin output low\n");
	gkt_gpio_set_pin_level(GKT_CONFIG_FACE_ID_POWER_CTRL_PIN, !GKT_CONFIG_FACE_ID_POWER_CTRL_PIN_ENABLE);
#endif	

#if defined(GKT_CONFIG_ADC_MOTOR_LOCKED_PIN)
	retval = gkt_gpio_config(GKT_CONFIG_ADC_MOTOR_LOCKED_PIN, GKT_GPIO_F_INPUT|GKT_GPIO_F_INPUT_PULL_DOWN, NULL);
	if (retval != GKT_SUCCESS) {	
		gkt_error("adc_standby: config adc motor locked pin failed :%d\n", retval);
		return retval;
	}
#endif	

#if defined(GKT_CONFIG_ADC_DETECT_PIN)
	retval = gkt_gpio_config(GKT_CONFIG_ADC_DETECT_PIN, GKT_GPIO_F_OUTPUT, NULL);
	if (retval != GKT_SUCCESS) {	
		gkt_error("adc_standby: config detect pin failed :%d\n", retval);
		return retval;
	}
	gkt_gpio_set_pin_level(GKT_CONFIG_ADC_DETECT_PIN, 0);
#endif

	return retval;
}


int gkt_adc_start(uint8_t dev_id, int *adc_value)
{
	int retval;
	adc_instance_s *instance;
	
	instance = &s_adc.instances[dev_id];
	if (!(instance->status & ADC_ST_CONFIG)) {
		gkt_error("gkt_adc_start: adc-%u not config!\n", dev_id);
		return GKT_ENOTOPEN;
	}
	instance->status |= ADC_ST_ENABLE;
	
	retval = G_ADC_GetVoltage(instance->channel);//ADC_GPIO_CHANNEL_0
	if (retval != -1)
		*adc_value = retval;
	else
		return -1;
	return GKT_SUCCESS;
}

int gkt_adc_config(uint8_t dev_id, gkt_adc_attr_s *adc_attr)
{
	adc_instance_s *instance;
	gkt_gpio_padmux_desc_s c_desc;
	G_ADC_InitTypeDef adc_struct;

	instance = &s_adc.instances[dev_id];
	if (!(instance->status & ADC_ST_INIT)) {
		gkt_error("gkt_adc_config: adc-%u not init!\n", dev_id);
		return GKT_ENOTINIT;
	}

	 /*clk*/
    G_SYSCTRL_PER3AdacClkCtrl(PLL_HSI_48M, SYSCTRL_CLK_Div2);

    //G_GPIO_Config(GPIOA, GPIO_Pin_3, ANALOG);
	c_desc.pin = GKT_CONFIG_ADC_PAD;
	c_desc.func = ANALOG;
	gkt_gpio_padmux_config(&c_desc);
	instance->status |= ADC_ST_CONFIG;
	instance->channel = adc_attr->channel;
	
    adc_struct.EN = ENABLE;
    adc_struct.ContModeEn = ENABLE;
    adc_struct.Volt_Trans_En = DISABLE;
    adc_struct.Mode = adc_attr->mode;
    adc_struct.Scale = adc_attr->scale_type;
    G_ADC_Init(&adc_struct);
	
	instance->scale_type = adc_attr->scale_type;
	instance->mode = adc_attr->mode;
	G_ADC_ModeConfig(adc_attr->mode, adc_attr->scale_type);//ADC_HVIN, ADC_Scale_700Mv
	
	return GKT_SUCCESS;
}

int gkt_adc_init(void)
{
	int dev_id, retval = GKT_EPERMIT;
	adc_instance_s *instance;
	
	gkt_trace("adc_init: ...\n");	
	if (!s_adc.init) {
		for (dev_id = 0; dev_id < GKT_ADC_MAXNUMS; dev_id++) {
			// TODO: setup irq (clear pending & enable) 
			instance = &s_adc.instances[dev_id];
			instance->dev_id = dev_id;
		}

#if defined(GKT_CONFIG_ADC_CTRL_PIN) 
		retval = gkt_gpio_config(GKT_CONFIG_ADC_CTRL_PIN, 
							GKT_GPIO_F_OUTPUT, NULL);
		if (retval != GKT_SUCCESS) {
			gkt_error("adc init: config ctrl pin failed: %d\n", retval);
		}

		retval = gkt_gpio_set_pin_level(GKT_CONFIG_ADC_CTRL_PIN, 1);
		if (retval != GKT_SUCCESS) {
			gkt_error("adc init: set ctrl pin level failed: %d\n", retval);
		}
#endif
		instance->status = ADC_ST_INIT;
		s_adc.init = 1;
	}
	return retval;
}

GKT_DEVICE_DESC_DEFINE(adc, NORMAL);

